package com.artwork.controller;

import com.artwork.common.domain.AjaxResult;
import com.artwork.common.enums.JsonResultEnum;
import com.artwork.common.exception.JsonException;
import com.artwork.model.Article;
import com.artwork.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * @Author JoJo
 * @Data 2021/9/8$ 14:46$
 * @Description
 * @Param $
 * @Return $
 */
@RestController
@RequestMapping("/admin/article")
public class ArticleController {
    @Autowired
    ArticleService articleService;

    /**
     * @desc 新增文章
     * @param article
     * @return
     */
    @PostMapping("/add")
    public AjaxResult addArticle(Article article){
        article.setCreateTime(System.currentTimeMillis());
        articleService.addArticle(article);
        return AjaxResult.success();
    }
    /**
     * @desc 修改文章
     * @param article
     * @return
     * @throws JsonException
     */
    @PostMapping("/update")
    public AjaxResult updateArticle(Article article) throws JsonException {
        Article article1 = articleService.getArticleById(article.getId());
        if (article1 == null){
            throw new JsonException(JsonResultEnum.TARGET_NULL);
        }
        article.setCreateTime(System.currentTimeMillis());
        articleService.updateArticle(article);
        return AjaxResult.success();
    }

    /**
     * @desc 修改文章状态
     * @param id
     * @param status
     * @return
     * @throws JsonException
     */
    @PostMapping("/status")
    public AjaxResult updateArticleStatus(int id, int status) throws JsonException {
        Article article = articleService.getArticleById(id);
        if (article == null){
            throw new JsonException(JsonResultEnum.TARGET_NULL);
        }
        articleService.pushArticle(id, status);
        return AjaxResult.success();
    }

    @PostMapping("/delete")
    public AjaxResult deleteArticle(int id) throws JsonException {
        Article article = articleService.getArticleById(id);
        if (article == null){
            throw new JsonException(JsonResultEnum.TARGET_NULL);
        }
        articleService.deleteArticle(id);
        return AjaxResult.success();
    }
}
